package com.yu.DaoImpl;

import com.yu.Dao.GoodDao;
import com.yu.Pojo.Goods;
import com.yu.utils.Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodDaoImpl implements GoodDao {
    public List<Goods> selectAllGoods () throws Exception {
        //创建连接
        Connection con = Util.reCon ();
        //输入sql
        String sql = "select * from goods";
        //执行sql preparestatment
        PreparedStatement ps = con.prepareStatement (sql);

        ResultSet rs = ps.executeQuery ();
        //创建一个list，装填每一个goods 信息
        List<Goods> goodsLists = new ArrayList<Goods> ();
        while (rs.next ())
        {
            int id = rs.getInt (1);
            String productName = rs.getString (2);
            String productDesc = rs.getString (3);
            String productUnit = rs.getString (4);
            String productCount = rs.getString (5);
            String totalPrice = rs.getString (6);

            Goods goods = new Goods (id, productName, productDesc, productUnit, productCount, totalPrice);

            goodsLists.add (goods);
        }

        Util.closeIO (con, (com.mysql.jdbc.PreparedStatement) ps,rs);


        return goodsLists;
    }



}
